/*
 * charger-ss805x.c
 *
 * charger driver.
 *
 * Copyright (C) 2024 Sinh Micro, Inc.
 * Subject to the GNU Public License, version 2.
 *
 * Author: lixiang<lixiang@sinhmicro.com>
 * 
 * Encoding format: GB2312
 * Version: v1.2.2
 * Date: 2024-11-05
 */

#include "hal-config.h"
#include "hal-regs.h"
#include "types.h"
#include "charger.h"
#include "hal/inc/gpio.h"

#if (CONFIG_USING_CHARGER == 1 && (defined(CONFIG_SYS_CHIP_SERIES_SS805X)))

#include "charger-ss805x-priv.h"
#include "adc.h"
#include "power.h"
#include "debug.h"
#include "ntc.h"
#include "ui.h"
#include "ui_event.h"

bool chg_abn_temp;
bool ovp_flag;
uint8_t ovp_deb;

#define OVP_GET_ADC_VALUE()     (adc_compute(adc_get_value(0x04)))


void charger_handle_100ms(void)
{
    uint16_t vbat_val;
    
    GPIO_MFP_P11(MFP_P11_AN4);
    ADCCHEN0 |= (1 << 4);

    // debug_print("  %d",ovp_deb);
    // debug_print("  %d",POWER_IS_DCIN_OK());
    // debug_print("  %d",CHARGER_IS_ENABLE());
    // debug_print("  %d\n",OVP_GET_ADC_VALUE());

    if (POWER_IS_DCIN_OK()) {
        ovp_flag = false;
        ovp_deb = 0;

        vbat_val = adc_get_vbat();
        if (CHARGER_IS_ENABLE() && !CHARGER_IS_CHGON() &&   \
            vbat_val < CONFIG_CHARGER_RC_VOL) {
            /*
            * if charger is enabled, but it is not charging(maybe because of charger timeout),
            * AND, if VBAT < CONFIG_CHARGER_RC_VOL, disable and re-enable(next 100ms) charger,
            * which can re-enable the charging procedure.
            */
            CHARGER_DISABLE();
            chg_abn_temp = false;
        } else {
            if(vbat_val < 2800){
                if(NTC_LEVEL_READ > 1 && NTC_LEVEL_READ < 5){
                    if(vbat_val < 2200){
                        CHGCON1 &= ~0x20;
                        CHGCON2 |= 0x40;
                        CHGCON3 = CHGCON3 & (~0xf8) | 0x10;         /* 150mA 0.1c 15mA */
                    }else{
                        CHGCON1 |= 0x20;
                        CHGCON2 &= ~0x40;
                        CHGCON3 = CHGCON3 & (~0xf8) | 0x30;         /* 350mA 1/4 87mA */
                    }
                    CHARGER_ENABLE();
                    chg_abn_temp = false;
                }else{
                    CHARGER_DISABLE();
                    if(!chg_abn_temp){
                        chg_abn_temp = true;
                        ui_event |= 1 << UI_EVENT_NTC_ABN;
                    }
                }
            }else{
                CHGCON1 |= 0x20;
                switch (NTC_LEVEL_READ)
                {
                    case 2:
                        CHGCON2 |= 0x40;
                        CHGCON3 = CHGCON3 & (~0xf8) | 0x20;         /* 250mA */
                        if(chg_abn_temp == false){
                            CHARGER_ENABLE();
                            if(vbat_val >= 4150){
                                chg_abn_temp = true;
                                // ui_event |= 1 << UI_EVENT_NTC_ABN;
                            }
                        }else{
                            CHARGER_DISABLE();
                            if(vbat_val <= 4050){
                                chg_abn_temp = false;
                            }
                        }
                        break;

                    case 3:
                        CHGCON2 |= 0x40;
                        CHGCON3 = CHGCON3 & (~0xf8) | CHGCON3_INIT_VALUE;       /* 450mA */
                        CHARGER_ENABLE();
                        chg_abn_temp = false;
                        break;

                    case 4:
                        CHGCON2 &= ~0x40;
                        CHGCON3 = CHGCON3 & (~0xf8) | 0x30;                     /* 350mA 1/4 87mA */
                        CHARGER_ENABLE();
                        chg_abn_temp = false;
                        break;

                    default:
                        if(!chg_abn_temp){
                            chg_abn_temp = true;
                            ui_event |= 1 << UI_EVENT_NTC_ABN;
                        }
                        CHARGER_DISABLE();
                        break;
                }
                
            }
        }
    } else {
        if(OVP_GET_ADC_VALUE() >= 1300 && ovp_flag == false){
            ovp_deb ++;
            if(ovp_deb >= 3){
                ovp_deb = 0;
                ovp_flag = true;
                ui_event |= 1 << UI_EVENT_NTC_ABN;
            }
        }else if(OVP_GET_ADC_VALUE() <= 1200 && ovp_flag == true){
            ovp_deb ++;
            if(ovp_deb >= 3){
                ovp_deb = 0;
                ovp_flag = false;
            }
        }else{
            ovp_deb = 0;
        }
        CHARGER_DISABLE();
        chg_abn_temp = false;
    }
}

#endif